package com.controller;

import com.entity.User;
import com.github.pagehelper.PageInfo;
import com.service.UserService;
import com.util.AjaxResult;
import com.util.RandomValidateCode;
import com.vo.FindBackUserVO;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 陈忠杰
 * @Date: 2021/07/02/15:35
 * @Description:
 */
@RestController
@RequestMapping("user")
public class UserController {

//    private static Logger logger = (Logger) LoggerFactory.getLogger(UserController.class);

    private static final Logger logger = Logger.getLogger(UserController.class);

//    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    UserService userService;

    @RequestMapping("getCode.do")
    public void getCode(HttpServletRequest request, HttpServletResponse response) {
        //设置相应类型,告诉浏览器输出的内容为图片
        response.setContentType("image/jpeg");
        //设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            logger.info("准备输出验证码");
            randomValidateCode.getRandcode(request, response);//输出图片方法
        } catch (Exception e) {
            logger.error("验证码发送失败，错误信息是：",e);
        }
    }

    @RequestMapping("loginBack.do")
    public AjaxResult loginBack(String login_name, String password,String code,HttpServletRequest request,HttpServletResponse response) {
        HttpSession session = request.getSession();
        boolean user = userService.isUser(login_name, password);
        AjaxResult ajaxResult = new AjaxResult();
        String randomcode = (String)session.getAttribute("randomcode_key");
        System.out.println("生成的随机验证码为"+randomcode);
        boolean random = code.equals(randomcode);
        ajaxResult.setSuccess(user&&random);
        if (user&&random) {
            ajaxResult.setMsg("登录成功！");
            session.setAttribute("isLogin","1");
            logger.info("将登录成功的状态'isLogin'设置为'1'");
        } else {
            ajaxResult.setMsg("登录失败，请重新验证！");
            logger.info("登录失败");
        }
        ajaxResult.setObj(login_name);
        return ajaxResult;
    }

    @RequestMapping("findBackUser.do")
    public PageInfo<User> findBackUser(FindBackUserVO findBackUserVO) {
        PageInfo<User> userByInfo = userService.getUserByInfo(findBackUserVO);
        logger.info("'findBackUser.do'分页查找成功！");
        return userByInfo;
    }

    @RequestMapping("showBackUser.do")
    public AjaxResult showBackUser(int id) {
        AjaxResult ajaxResult = new AjaxResult();
        User userById = userService.getUserById(id);
        ajaxResult.setObj(userById);
        return ajaxResult;
    }

    @RequestMapping("modifyBackUser.do")
    public void modifyBackUser(User user) {
        userService.changeUserByInfo(user);
    }

    @RequestMapping("modifyBackUserStatus.do")
    public AjaxResult modifyBackUserStatus(User user) {
        AjaxResult ajaxResult = new AjaxResult();
        userService.changeUserStatus(user);
        return ajaxResult;
    }
}
